﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundUnderviser.ModelLayer;

namespace SundUnderviser.DBLayer
{
    public class DBLektionsTemperatur
    {
        private SqlCommand dbCmd = null;
      
       
        public List<Temperatur> HentAlleTempMaalinger(int pnr)
        {
            List<Temperatur> returnList = new List<Temperatur>();
            string sql = @"select patientnr, dato, vagt, tidspunkt, temperatur, blodtryk, puls, respiration, initialer from Lektionstemperatur "
                         + " where patientnr = " + pnr 
                         + " order by dato desc , tidspunkt desc";


            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();

            Temperatur tempObj;

            while (dbReader.Read())
            {

                tempObj = buildTemperaturObj(dbReader);
                returnList.Add(tempObj);
            }
            DBConnection.Close();
            return returnList;

        }




     
        private Temperatur buildTemperaturObj(IDataReader dbReader)
        {
            Temperatur tempObj = new Temperatur();
            try
            {
                Console.WriteLine("antal felter " + dbReader.FieldCount);
                Patient patientObj = new Patient();
                patientObj.Nr = Convert.ToInt32(dbReader["patientnr"].ToString());
                tempObj.Patient = patientObj;
                tempObj.Date = Convert.ToDateTime(dbReader["dato"].ToString());
                tempObj.Vagt = dbReader["vagt"].ToString();
                tempObj.Tid = dbReader["tidspunkt"].ToString();
                tempObj.Temp = Convert.ToDecimal(dbReader["temperatur"].ToString());
                tempObj.Puls = Convert.ToInt32(dbReader["puls"].ToString());
                tempObj.Blodtryk = dbReader["blodtryk"].ToString();
                tempObj.Respiration = dbReader["respiration"].ToString();
                tempObj.Initialer = dbReader["initialer"].ToString();


            }
            catch (Exception ex)
            {

                Console.WriteLine(" lektions temperatur objekt ikke oprettet korrekt");
                Console.WriteLine(ex);
               tempObj = null;
            }

            return tempObj;
        }
        public int InsertTemperatur(Temperatur tempObj)
        {
            int rc = -1;
         
            try
            {

                DBConnection.Close();
                string myDato = "" + tempObj.Date.Year;
                if (tempObj.Date.Month >= 10)
                {
                    myDato = myDato + tempObj.Date.Month;
                }
                else
                {
                    myDato = myDato + "0" + tempObj.Date.Month;
                }

                if (tempObj.Date.Day >= 10)
                {
                    myDato = myDato + tempObj.Date.Day;
                }
                else
                {
                    myDato = myDato + "0" + tempObj.Date.Day;
                }

                string temp = tempObj.Temp.ToString();
                decimal tempD = Convert.ToDecimal(temp);
                 string sql = " insert into lektionstemperatur (patientnr, dato,vagt,tidspunkt,  blodtryk, puls, respiration, initialer)" +
                           "values ( " + tempObj.Patient.Nr + ",'"
                               + myDato + "','"
                               + tempObj.Vagt + "','" 
                               + tempObj.Tid + "','"
                             //  + tempD + "','"
                               + tempObj.Blodtryk + "',"
                               + tempObj.Puls + ",'"
                               + tempObj.Respiration + "','"
                               + tempObj.Initialer +  "')";

                 Console.WriteLine("Sql insert temp " + sql);
                 dbCmd = DBConnection.GetDbCommand(sql);

                rc = dbCmd.ExecuteNonQuery();
                DBConnection.Close(); 

            }
            catch (Exception insertE)
            {
                Console.WriteLine("Fejl i insert lektionstemperatur");
                Console.WriteLine(insertE);
            }
            DBConnection.Close();
            return rc;
        }
     

    }
}
